#include "tour.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/glut.h> 
#include <time.h>


GLfloat ambientLight[] = { 0.7, 0.6, 1.0, 1.0f};
GLfloat spotLight[] = { 0, 1, 0, 1.0f};
GLfloat lightPos[] = { 0.0f, 5000.0f, 0.0f, 1.0f };

int main(int argc,char* argv[])
{

        init();

        glutInitWindowPosition(250,150);        // define position of window in the screen
        glutInitWindowSize(600,600);    // define the window size

        glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA | GLUT_DOUBLE);
        glutCreateWindow("HW4"); // creats the window and geve it a name
                
        glDepthFunc(GL_LESS);
                
        glutReshapeFunc(reshape);
        glutDisplayFunc(renderCallBack); // passes the drawing function reference
        glutKeyboardFunc(regkeyPress);  
        glutMainLoop(); 
}


void reshape(int width, int height){
        float AR,Left,Right,Top,Bottom;
        
        Left    = -5;
        Right   =  5;
    Top         =  5;
    Bottom      = -5;
        
        glViewport(0,0,width,height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        AR =(float)width/height;
        
        if (AR>=1)
        {
                Left=Left*AR;
                Right=Right*AR;
        }else{
                Top=Top/AR;
                Bottom=Bottom/AR;
        }
        gluPerspective(50,AR,0.5,60);
}


void renderCallBack(void) 
{
        GLfloat pos[4]; 
		GLfloat direc[4];
        Point vec=getnvect(&Eye,&Center,1.6);
		vec=getvect(&Center,&vec);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        glClearColor(1,1,1,1); // set bakground color
        glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); //erase bakground with selected color
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_NORMALIZE);

        glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
        
        glLightfv(GL_LIGHT0,GL_DIFFUSE,ambientLight);
        glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
        glEnable(GL_LIGHT0);

		//glRotated(5.0,0,1,0);
	
		glLightfv(GL_LIGHT1,GL_DIFFUSE ,spotLight);

		pos[0]=vec.x;
        pos[1]=Center.y;
        pos[2]=vec.z;
		pos[3]=1;
		glLightfv(GL_LIGHT1,GL_POSITION, pos);


		direc[0]=16;
	   	glLightfv(GL_LIGHT1,GL_SPOT_CUTOFF,direc);   

		glEnable(GL_LIGHT1);
        glEnable(GL_LIGHTING);


        gluLookAt(Eye.x,Eye.y+3.5,Eye.z,Center.x,Center.y,Center.z,Center.x,Center.y+50000,Center.z);   //will have to change

        DrawWorld();

        glutSwapBuffers();
        glDisable(GL_DEPTH_TEST);
}


void regkeyPress(unsigned char key, int x, int y)
{
        Point a;
        switch(key)
        {
        case 'w':
                a=getnvect(&Center,&Eye,0.3);
                movePoint(&Center,&a);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
#ifndef SUPERMAN
				Eye.y-=a.y;
				Up.y-=a.y;
				Center.y-=a.y;
#endif
                break;
                
        case 's':
                a=getnvect(&Eye,&Center,0.3);
                movePoint(&Center,&a);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
#ifndef SUPERMAN
				Eye.y-=a.y;
				Up.y-=a.y;
				Center.y-=a.y;
#endif
                break;
        case 'a':
                a=vectmul(getnvect(&Eye,&Center,1),getnvect(&Eye,&Up,1),-0.3);
                movePoint(&Center,&a);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
#ifndef SUPERMAN
				Eye.y-=a.y;
				Up.y-=a.y;
				Center.y-=a.y;
#endif                
				break;
        case 'd':
                a=vectmul(getnvect(&Eye,&Center,1),getnvect(&Eye,&Up,1),0.3);
                movePoint(&Center,&a);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
#ifndef SUPERMAN
				Eye.y-=a.y;
				Up.y-=a.y;
				Center.y-=a.y;
#endif
				break;
        
        
        
        case 'i':
                a=getnvect(&Up,&Eye,0.3);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
                break;
                
        case 'k':
                a=getnvect(&Eye,&Up,0.3);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
                break;
        case 'j':
                a=vectmul(getnvect(&Eye,&Center,1),getnvect(&Eye,&Up,1),-0.3);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
                break;
        case 'l':
                a=vectmul(getnvect(&Eye,&Center,1),getnvect(&Eye,&Up,1),0.3);
                movePoint(&Eye,&a);
                movePoint(&Up,&a);
                break;
        }
        glutPostRedisplay();
}
